/* Copyright (c) 2025 Beijing Semidrive Technology Corporation
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/** *****************************************************************************************************
 *  \file     Pwm_Ip_Xtrg.h                                                                             *
 *  \brief    This file contains interface header for PWM MCAL driver                                   *
 *                                                                                                      *
 * <table>                                                                                              *
 * <tr><th>Date           <th>Version                                                                   *
 * <tr><td>2024/02/20     <td>3.0.0                                                                     *
 * </table>                                                                                             *
 *******************************************************************************************************/
#ifndef PWM_IP_XTRG_H
#define PWM_IP_XTRG_H

#ifdef __cplusplus
extern "C" {
#endif

/********************************************************************************************************
 *                                      Include header files                                            *
 *******************************************************************************************************/
#include "Mcu.h"
#include "Mcu_Cfg.h"
#include "Pwm_Cfg.h"

/********************************************************************************************************
 *                                 Global Macro definition                                              *
 *******************************************************************************************************/
/** \brief  The base addrsss of Xtrg1-Pwm controller */
#define PWM_IP_XTRG1_BASE                                       (0xf8170000UL)
/** \brief  The base addrsss of Xtrg1-Pwm controller */
#define PWM_IP_XTRG2_BASE                                       (0xf8180000UL)
/* Xtrg-Pwm interrupt status register */
#define PWM_IP_XTRG_OVF_INT_STA_REG                             (0x1400UL)
/* Xtrg-Pwm interrupt status enable register */
#define PWM_IP_XTRG_OVF_INT_STA_EN_REG                          (0x1404UL)
/* Xtrg-Pwm interrupt signal enable register */
#define PWM_IP_XTRG_OVF_INT_SIG_EN_REG                          (0x1408UL)
#define PWM_IP_XTRG_INT_CMP_EN(ch)                              (1UL << (ch))
#define PWM_IP_XTRG_INT_CNT_OVF_EN(ch)                          (1UL << (ch))
/* Xtrg-Pwm config register */
#define PWM_IP_XTRG_CFG_REG                                     (0x140cUL)
#define PWM_IP_XTRG_CLK_GT_EN                                   (1UL)
#define PWM_IP_XTRG_CLK_GT_DIS                                  (0UL)
#define PWM_IP_XTRG_SW_RST_OFFSET                               (1UL)
#define PWM_IP_XTRG_SW_RST_EN                                   (1UL)
/* Xtrg-Pwm counter enable register */
#define PWM_IP_XTRG_CNT_EN_REG                                  (0x1410UL)
#define PWM_IP_XTRG_CNT_EN_CH(ch)                               (1UL << (ch))
#define PWM_IP_XTRG_CNT_ENABLE_ALL                              (0xffffffffUL)
#define PWM_IP_XTRG_CNT_DIABLE_ALL                              (0UL)
/* Xtrg-Pwm interrupt status enable bits */
#define PWM_IP_XTRG_INT_EN_CMP_0_31                             (0xffffffffUL)
#define PWM_IP_XTRG_INT_STA_EN                                  (PWM_IP_XTRG_INT_EN_CMP_0_31)
/* Xtrg-Pwm interrupt signal enable bits */
#define PWM_IP_XTRG_SIG_EN_CMP_0_31                             (0xffffffffUL)
#define PWM_IP_XTRG_INT_SIG_EN                                  (PWM_IP_XTRG_SIG_EN_CMP_0_31)
/* Xtrg-Pwm channel config register */
#define PWM_IP_XTRG_CH_CFG_REG                                  (0x1418UL)
#define PWM_IP_XTRG_CH_CFG_REG_GAP                              (4UL)
#define PWM_IP_XTRG_CH_CFG_N(ch)            (PWM_IP_XTRG_CH_CFG_REG +                       \
                                                ((ch) * PWM_IP_XTRG_CH_CFG_REG_GAP))
#define PWM_IP_XTRG_CNT_INTV_SEL_OFFSET                         (1UL)
#define PWM_IP_XTRG_CNT_INTV_SEL_MASK                           (7UL)
#define PWM_IP_XTRG_IO_MUX_SEL_OFFSET                           (8UL)
#define PWM_IP_XTRG_IO_MUX_SEL_MASK                             (3UL)
#define PWM_IP_XTRG_CMP_POL_OFFSET                              (0UL)
#define PWM_IP_XTRG_CMP_POL_EN                                  (1UL)
/* Xtrg-Pwm compare value register */
#define PWM_IP_XTRG_CMP_VALUE_REG                               (0x1520UL)
#define PWM_IP_XTRG_CMP_VALUE_REG_GAP                           (4UL)
#define PWM_IP_XTRG_CMP_VALUE_N(ch)         (PWM_IP_XTRG_CMP_VALUE_REG +                       \
                                                ((ch) * PWM_IP_XTRG_CMP_VALUE_REG_GAP))
#define PWM_IP_XTRG_CMP_EN_OFFSET                               (16UL)
/* Xtrg-Pwm counter overflow value register */
#define PWM_IP_XTRG_CNT_OVF_REG                                 (0x1620UL)
#define PWM_IP_XTRG_CNT_OVF_REG_GAP                             (4UL)
#define PWM_IP_XTRG_CNT_OVF_N(ch)           (PWM_IP_XTRG_CNT_OVF_REG +                       \
                                                ((ch) * PWM_IP_XTRG_CNT_OVF_REG_GAP))
#define PWM_IP_XTRG_CNT_EN_OFFSET                               (16UL)
/** \brief  Xtrg-Pwm module maximum number */
#define PMW_IP_XTRG_MODULE_MAX_NUMBER                           (2U)
/** \brief  Xtrg-Pwm counter/compare maximal value */
#define PWM_IP_XTRG_COUNTER_VALUE_MAX                           (0xffffUL)
#define PWM_IP_XTRG_COMPARE_VALUE_MAX                           (0xffffUL)
#define PWM_IP_XTRG_COUNTER_INTV_NUM                            (8UL)
#define PWM_IP_XTRG_COUNTER_INTV_MAX                            (16000UL)
#define PWM_IP_XTRG_COUNTER_CFG_OFFSET                          (8UL)

/********************************************************************************************************
 *                                  Global Function Declarations                                        *
 *******************************************************************************************************/

/** *****************************************************************************************************
 * \brief Assignment Pwm_Handler base address of Xtrg-Pwm registers.
 *
 * \verbatim
 * Syntax             : void Pwm_Ip_XtrgSetBaseAddr(
 *                          Pwm_HandlerConfigType* coreHandlerPtr)
 *
 * Service ID[hex]    : None
 *
 * Sync/Async         : Synchronous
 *
 * Reentrancy         : Non reentrant
 *
 * Parameters (in)    : coreHandlerPtr - Pwm handler configutations.
 *
 * Parameters (inout) : None
 *
 * Parameters (out)   : None
 *
 * Return value       : None
 *
 * Description        : Assignment Pwm_Handler base address of Xtrg-Pwm registers.
 *
 * \endverbatim
 * Traceability       : SWSR_PWM_025
 *******************************************************************************************************/
void Pwm_Ip_XtrgSetBaseAddr(Pwm_HandlerConfigType* coreHandlerPtr);

/** *****************************************************************************************************
 * \brief Xtrg-Pwm IP initialization.
 *
 * \verbatim
 * Syntax             : Std_ReturnType Pwm_Ip_XtrgInit(
 *                          Pwm_HandlerConfigType *coreHandlerPtr,
 *                          const Pwm_ConfigType* configPtr)
 *
 * Service ID[hex]    : None
 *
 * Sync/Async         : Synchronous
 *
 * Reentrancy         : Non reentrant
 *
 * Parameters (in)    : coreHandlerPtr  - Pwm handler configutations.
 *                      configPtr       - Pointer to Pwm configuration set.
 *
 * Parameters (inout) : None
 *
 * Parameters (out)   : None
 *
 * Return value       : E_OK    - Pwm IP initialization successful.
 *                      Others  - Pwm IP initialization failed.
 *
 * Description        : Xtrg-Pwm IP initialization.
 *
 * \endverbatim
 * Traceability       : SWSR_PWM_025 SWSR_PWM_026 SWSR_PWM_027 SWSR_PWM_028 SWSR_PWM_029 SWSR_PWM_030
 *                      SWSR_PWM_093 SWSR_PWM_094
 *******************************************************************************************************/
Std_ReturnType Pwm_Ip_XtrgInit
(
    Pwm_HandlerConfigType *coreHandlerPtr,
    const Pwm_ConfigType* configPtr
);

#if (STD_ON == PWM_DE_INIT_API)
/** *****************************************************************************************************
 * \brief Xtrg-Pwm IP De-Initialization.
 *
 * \verbatim
 * Syntax             : Std_ReturnType Pwm_Ip_XtrgDeinit(
 *                          const Pwm_HandlerConfigType* coreHandlerPtr)
 *
 * Service ID[hex]    : None
 *
 * Sync/Async         : Synchronous
 *
 * Reentrancy         : Non reentrant
 *
 * Parameters (in)    : coreHandlerPtr  - Pwm handler configutations.
 *
 * Parameters (inout) : None
 *
 * Parameters (out)   : None
 *
 * Return value       : E_OK    - Pwm IP De-Initialization successful.
 *                      Others  - Pwm IP De-Initialization failed.
 *
 * Description        : Xtrg-Pwm IP De-Initialization.
 *                      This function is available if the PWM_DE_INIT_API is set STD_ON.
 *
 * \endverbatim
 * Traceability       : SWSR_PWM_036 SWSR_PWM_037 SWSR_PWM_038 SWSR_PWM_094
 *******************************************************************************************************/
Std_ReturnType Pwm_Ip_XtrgDeinit(const Pwm_HandlerConfigType* coreHandlerPtr);
#endif /** #if (STD_ON == PWM_DE_INIT_API) */

#if (STD_ON == PWM_SET_DUTY_CYCLE_API)
/** *****************************************************************************************************
 * \brief Set the duty cycle of the Xtrg-Pwm channel.
 *
 * \verbatim
 * Syntax             : Std_ReturnType Pwm_Ip_XtrgSetDutyCycle(
 *                          const Pwm_HandlerConfigType* coreHandlerPtr,
 *                          Pwm_ChannelType channelNumber,
 *                          uint16 dutyCycle)
 *
 * Service ID[hex]    : None
 *
 * Sync/Async         : Synchronous
 *
 * Reentrancy         : Reentrant
 *
 * Parameters (in)    : coreHandlerPtr  - Pwm handler configutations.
 *                      channelNumber   - Numeric identifier of the PWM channel.
 *                      dutyCycle       - The duty of the PWM channel., Min=0x0000 Max=0x8000.
 *
 * Parameters (inout) : None
 *
 * Parameters (out)   : None
 *
 * Return value       : E_OK    - Set duty cycle successful.
 *                      Others  - Set duty cycle failed.
 *
 * Description        : Set the duty cycle of the Xtrg-Pwm channel.
 *                      This function is available if the PWM_SET_DUTY_CYCLE_API is set STD_ON.
 *
 * \endverbatim
 * Traceability       : SWSR_PWM_006 SWSR_PWM_007 SWSR_PWM_015 SWSR_PWM_042 SWSR_PWM_043 SWSR_PWM_044
 *                      SWSR_PWM_045 SWSR_PWM_094
 *******************************************************************************************************/
Std_ReturnType Pwm_Ip_XtrgSetDutyCycle
(
    const Pwm_HandlerConfigType* coreHandlerPtr,
    Pwm_ChannelType channelNumber,
    uint16 dutyCycle
);
#endif /** #if (STD_ON == PWM_SET_DUTY_CYCLE_API) */

#if (STD_ON == PWM_SET_PERIOD_AND_DUTY_API)
/** *****************************************************************************************************
 * \brief Set the period and the duty cycle of the Xtrg-Pwm channel.
 *
 * \verbatim
 * Syntax             : Std_ReturnType Pwm_Ip_XtrgSetPeriodAndDuty(
 *                          Pwm_HandlerConfigType* coreHandlerPtr,
 *                          Pwm_ChannelType channelNumber,
 *                          Pwm_PeriodType period,
 *                          uint16 dutyCycle)
 *
 * Service ID[hex]    : None
 *
 * Sync/Async         : Synchronous
 *
 * Reentrancy         : Reentrant
 *
 * Parameters (in)    : coreHandlerPtr  - Pwm handler configutations.
 *                      channelNumber   - Numeric identifier of the PWM channel.
 *                      period          - Period of the PWM signal.
 *                      dutyCycle       - The duty of the PWM channel., Min=0x0000 Max=0x8000.
 *
 * Parameters (inout) : None
 *
 * Parameters (out)   : None
 *
 * Return value       : E_OK    - Set period and the duty cycle successful.
 *                      Others  - Set period and the duty cycle failed.
 *
 * Description        : Set the period and the duty cycle of the Xtrg-Pwm channel.
 *                      This function is available if the PWM_SET_PERIOD_AND_DUTY_API is set STD_ON.
 *
 * \endverbatim
 * Traceability       : SWSR_PWM_006 SWSR_PWM_007 SWSR_PWM_015 SWSR_PWM_043 SWSR_PWM_044 SWSR_PWM_045
 *                      SWSR_PWM_050 SWSR_PWM_051 SWSR_PWM_056 SWSR_PWM_094
 *******************************************************************************************************/
Std_ReturnType Pwm_Ip_XtrgSetPeriodAndDuty
(
    Pwm_HandlerConfigType* coreHandlerPtr,
    Pwm_ChannelType channelNumber,
    Pwm_PeriodType period,
    uint16 dutyCycle
);
#endif /** #if (STD_ON == PWM_SET_PERIOD_AND_DUTY_API) */

#if (STD_ON == PWM_SET_OUTPUT_TO_IDLE_API)
/** *****************************************************************************************************
 * \brief Set Xtrg-Pwm channel output status to idle.
 *
 * \verbatim
 * Syntax             : Std_ReturnType Pwm_Ip_XtrgSetOutputToIdle(
 *                          const Pwm_HandlerConfigType* coreHandlerPtr,
 *                          Pwm_ChannelType channelNumber)
 *
 * Service ID[hex]    : None
 *
 * Sync/Async         : Synchronous
 *
 * Reentrancy         : Reentrant
 *
 * Parameters (in)    : coreHandlerPtr  - Pwm handler configutations.
  *                     channelNumber   - Numeric identifier of the PWM channel.
 *
 * Parameters (inout) : None
 *
 * Parameters (out)   : None
 *
 * Return value       : E_OK    - Set PWM channel output status to idle successful.
 *                      Others  - Set PWM channel output status to idle failed.
 *
 * Description        : Set Xtrg-Pwm channel output status to idle.
 *                      This function is available if the PWM_SET_OUTPUT_TO_IDLE_API is set STD_ON.
 *
 * \endverbatim
 * Traceability       : SWSR_PWM_006 SWSR_PWM_007 SWSR_PWM_057 SWSR_PWM_094
 *******************************************************************************************************/
Std_ReturnType Pwm_Ip_XtrgSetOutputToIdle
(
    const Pwm_HandlerConfigType* coreHandlerPtr,
    Pwm_ChannelType channelNumber
);
#endif /** #if (STD_ON == PWM_SET_OUTPUT_TO_IDLE_API) */

#if (STD_ON == PWM_NOTIFICATION_SUPPORTED)
/** *****************************************************************************************************
 * \brief Disable notification of Xtrg-Pwm channel.
 *
 * \verbatim
 * Syntax             : void Pwm_Ip_XtrgDisableNotification(
 *                          const Pwm_HandlerConfigType* coreHandlerPtr,
 *                          Pwm_ChannelType channelNumber)
 *
 * Service ID[hex]    : None
 *
 * Sync/Async         : Synchronous
 *
 * Reentrancy         : Reentrant
 *
 * Parameters (in)    : coreHandlerPtr  - Pwm handler configutations.
 *                      channelNumber   - Numeric identifier of the PWM channel.
 *
 * Parameters (inout) : None
 *
 * Parameters (out)   : None
 *
 * Return value       : None
 *
 * Description        : Disable notification of Xtrg-Pwm channel.
 *                      This function is available if the PWM_NOTIFICATION_SUPPORTED is set STD_ON.
 *
 * \endverbatim
 * Traceability       : SWSR_PWM_006 SWSR_PWM_007 SWSR_PWM_080
 *******************************************************************************************************/
void Pwm_Ip_XtrgDisableNotification
(
    const Pwm_HandlerConfigType* coreHandlerPtr,
    Pwm_ChannelType channelNumber
);
#endif /** #if (STD_ON == PWM_NOTIFICATION_SUPPORTED) */

#if (STD_ON == PWM_NOTIFICATION_SUPPORTED)
/** *****************************************************************************************************
 * \brief Enable notification of Xtrg-Pwm channel.
 *
 * \verbatim
 * Syntax             : void Pwm_Ip_XtrgEnableNotification(
 *                          const Pwm_HandlerConfigType* coreHandlerPtr,
 *                          Pwm_ChannelType channelNumber,
 *                          Pwm_EdgeNotificationType notification)
 *
 * Service ID[hex]    : None
 *
 * Sync/Async         : Synchronous
 *
 * Reentrancy         : Reentrant
 *
 * Parameters (in)    : coreHandlerPtr  - Pwm handler configutations.
 *                      channelNumber   - Numeric identifier of the PWM channel.
 *
 * Parameters (inout) : None
 *
 * Parameters (out)   : None
 *
 * Return value       : None
 *
 * Description        : Enable notification of Xtrg-Pwm channel.
 *                      This function is available if the PWM_NOTIFICATION_SUPPORTED is set STD_ON.
 *
 * \endverbatim
 * Traceability       : SWSR_PWM_006 SWSR_PWM_007 SWSR_PWM_073 SWSR_PWM_074 SWSR_PWM_080
 *******************************************************************************************************/
void Pwm_Ip_XtrgEnableNotification
(
    const Pwm_HandlerConfigType* coreHandlerPtr,
    Pwm_ChannelType channelNumber,
    Pwm_EdgeNotificationType notification
);
#endif /** #if (STD_ON == PWM_NOTIFICATION_SUPPORTED) */

#if defined(LOWPOWER_SUPPORT_SUSPEND_RESUME)
/** *****************************************************************************************************
 * \brief Xtrg-Pwm IP suspend.
 *
 * \verbatim
 * Syntax             : Std_ReturnType Pwm_Ip_XtrgSuspend(
 *                          Pwm_HandlerConfigType* coreHandlerPtr)
 *
 * Service ID[hex]    : None
 *
 * Sync/Async         : Synchronous
 *
 * Reentrancy         : Non reentrant
 *
 * Parameters (in)    : coreHandlerPtr  - Pwm handler configutations.
 *
 * Parameters (inout) : None
 *
 * Parameters (out)   : None
 *
 * Return value       : E_OK    - Xtrg-Pwm IP suspend successful.
 *                      Others  - Xtrg-Pwm IP suspend failed.
 *
 * Description        : Suspend the Xtrg-Pwm IP, just only used for lowpower.
 *
 * \endverbatim
 * Traceability       : SWSR_GENERAL_22
 *******************************************************************************************************/
Std_ReturnType Pwm_Ip_XtrgSuspend(Pwm_HandlerConfigType* coreHandlerPtr);
#endif /* #if defined(LOWPOWER_SUPPORT_SUSPEND_RESUME) */

/** *****************************************************************************************************
 * \brief Xtrg-Pwm interrupt handler.
 *
 * \verbatim
 * Syntax             : void Pwm_Ip_XtrgIrq(
 *                          Pwm_XtrgControllerIrqType xtrgController)
 *
 * Service ID[hex]    : None
 *
 * Sync/Async         : Synchronous
 *
 * Reentrancy         : Reentrant
 *
 * Parameters (in)    : xtrgController - Pwm controller irq index: XTRG1_PWM_MODULE_IRQ_INDEX,
 *                                       XTRG2_PWM_MODULE_IRQ_INDEX
 *
 * Parameters (inout) : None
 *
 * Parameters (out)   : None
 *
 * Return value       : None
 *
 * Description        : Xtrg-Pwm interrupt handler, clear irq status and call notification function.
 *
 * \endverbatim
 * Traceability       : SWSR_PWM_006 SWSR_PWM_007 SWSR_PWM_089 SW_SM003 SW_SM004
 *******************************************************************************************************/
void Pwm_Ip_XtrgIrq(Pwm_XtrgControllerIrqType xtrgController);

#ifdef __cplusplus
}
#endif

#endif /* PWM_IP_XTRG_H */
/* End of file */
